﻿/***************************************************************************
 创建者: 华磊
 开始时间: 2019.9.20
 copyright: (C) 华友高科
 ***************************************************************************/
#include "monitorviewer.h"

#include <QWheelEvent>
#include <QDebug>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QMessageBox>
#include <QProgressDialog>
#include <QCoreApplication>
#include <QColorDialog>
#include "monitorscene.h"
#include "global.h"

MonitorViewer::MonitorViewer(QWidget *parent) :  QGraphicsView(parent)
{
    setRenderHint(QPainter::Antialiasing, false);
    setDragMode(QGraphicsView::RubberBandDrag);
    setOptimizationFlags(QGraphicsView::DontSavePainterState);
    setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setMouseTracking(true);
    setFrameStyle(QFrame::NoFrame);
    setDragMode( QGraphicsView::RubberBandDrag);
    setInteractive(true);

    translateSpeed=5;

//    setRenderHint(QPainter::Antialiasing, false);
//    setDragMode(QGraphicsView::RubberBandDrag);
//    setOptimizationFlags(QGraphicsView::DontSavePainterState);
//    setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
//    setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
//    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//    setMouseTracking(true);
//    setFrameStyle(QFrame::NoFrame);
}

void MonitorViewer::toggleSelectMode()
{
    setDragMode( QGraphicsView::RubberBandDrag);
    setInteractive(true);
}

void MonitorViewer::toggleDragMode()
{
    setDragMode(QGraphicsView::ScrollHandDrag);
    setInteractive(false);
}

void MonitorViewer::wheelEvent(QWheelEvent *event)
{
    static int kk = 0;
    double scaleFactor = 1.10;
    if (event->modifiers() & Qt::ControlModifier) {
        if (event->delta() > 0)
        {
//            if(kk<20){
//                ++kk;
                scale(scaleFactor, scaleFactor);
//            }
        }
        else
        {
//            if(kk>-20){
//                --kk;
                scale(1.0 / scaleFactor, 1.0 / scaleFactor);
//            }
        }
        viewport()->update();
        event->accept();
    } else {
        QGraphicsView::wheelEvent(event);
    }



}

// 上/下/左/右键向各个方向移动、加/减键进行缩放、空格/回车键旋转
void MonitorViewer::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Up:
        translateCenter(QPointF(0, -2));  // 上移
        break;
    case Qt::Key_Down:
        translateCenter(QPointF(0, 2));  // 下移
        break;
    case Qt::Key_Left:
        translateCenter(QPointF(-2, 0));  // 左移
        break;
    case Qt::Key_Right:
        translateCenter(QPointF(2, 0));  // 右移
        break;

    default:
        QGraphicsView::keyPressEvent(event);
    }
}

// 平移
void MonitorViewer::translateCenter(QPointF delta)
{
//    qreal x=delta.x()*translateSpeed;
//    qreal y=delta.y()*translateSpeed;
//    translate(x, y);
//    qDebug()<<x<<y;

    // 根据当前 zoom 缩放平移数
//       delta *= 1;
//       delta *= translateSpeed;

//       // view 根据鼠标下的点作为锚点来定位 scene
//       setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
//       QPoint newCenter(500 / 2 - delta.x(),  500 / 2 - delta.y());
//       centerOn(mapToScene(newCenter));

//       // scene 在 view 的中心点作为锚点
//       setTransformationAnchor(QGraphicsView::AnchorViewCenter);

}
